﻿/*
【说明】
本类为SPH转换器数据读取模块。

【版本】
2022/04/29 初始化创建

【参考资料】
SPH 转换规则文档  https://docs.qq.com/sheet/DWXRVYUtBd251eFVo?u=db936ce54f984d52a53926a39354cc49&tab=va4ty5
XML 读取方法     https://www.cnblogs.com/likui-bookHouse/p/11132756.html
XML 命名空间     https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode?view=net-6.0
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace Verifications.vers
{
    public class SPHAnalyzer
    {
        public SPHAnalyzer() { }
 

        public void ReadFile(string xmlfile=@".\data\sample.xml")
        {
            if (!File.Exists(xmlfile))
            {
                Console.WriteLine($"File {xmlfile} does not exist.");
                return;
            }

            // Console.WriteLine(File.ReadAllText(xmlfile));
            XmlDocument root = new XmlDocument();
            root.Load(xmlfile);

            XmlNamespaceManager nsm = new XmlNamespaceManager(root.NameTable);
            nsm.AddNamespace("sph", "http://www.sciemetric.com/namespace");

            
            Console.WriteLine("----------- k1002 model -----------");
            var k1002 = GetNodeText(root, "descendant::sph:model_number/sph:label", nsm); 
            Console.WriteLine(k1002);

            Console.WriteLine("\n----------- K0014 serial_number-----------");
            Console.WriteLine(GetNodeText(root, "descendant::sph:serial_number", nsm));

            var features = root.SelectNodes("//sph:feature", nsm);
            Console.WriteLine($"\n----------- features ({features.Count})-----------");
            foreach (XmlNode node in features)
            {
                // K2002 - label
                // K0004 date_stamp
                // lower/upper
                var k2001 = GetNodeText(node, "descendant::sph:label", nsm);
                var k2002 = k2001;
                var k2111 = GetNodeText(node, "descendant::sph:upper", nsm);
                var k2110 = GetNodeText(node, "descendant::sph:lower", nsm);
                var k2142 = k2001.Contains('_') ? k2001.Split('_')[1] : "";
                var k0001 = GetNodeText(node, "descendant::sph:value", nsm);
                var K0004 = GetNodeText(node, "descendant::sph:date_stamp", nsm);
                Console.WriteLine("-- feature --");
                Console.WriteLine($"k2001: {k2001}");
                Console.WriteLine($"k2002: {k2002}");
                Console.WriteLine($"k2111: {k2111}");
                Console.WriteLine($"k2110: {k2110}");
                Console.WriteLine($"k2142: {k2142}");
                Console.WriteLine($"k0001: {k0001}");
                Console.WriteLine($"K0004: {K0004}"); 
                Console.WriteLine();
                
            }

            Console.WriteLine("done");
        }

        public static string GetNodeText(XmlNode node, string path)
        {
            var n = node.SelectSingleNode(path);
            return n == null ? "" : n.InnerText;
        }

        public static string GetNodeText(XmlNode node, string path, XmlNamespaceManager nsm)
        {
            var n = node.SelectSingleNode(path, nsm);
            return n == null ? "" : n.InnerText;
        }
    }
}
